package com.example.controller;

import com.example.dao.LearningSituationCrudRepository;
import com.example.dao.UserCrudRepository;
import com.example.pojo.LearningSituation;
import com.example.pojo.PageBean;
import com.example.pojo.TestQuestion;
import com.example.pojo.User;
import com.example.pojo.vo.LearningSituationDTO;
import com.example.pojo.vo.LearningSituationVo;
import com.example.tools.JwtUtils;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/learningSituation")
@Slf4j
public class LearningSituationController {
    @Autowired
    private LearningSituationCrudRepository learningSituationCrudRepository;
    @Autowired
    private UserCrudRepository userCrudRepository;

    @PostMapping("/addLearningSituation")
    public Result add(Integer trainingPlanId, Integer documentVideoId, Integer percentage, @RequestHeader("token") String token) {
        Integer id = JwtUtils.parseJWT(token).get("id", Integer.class);
        User user = userCrudRepository.findById(id).get();
        LearningSituation save = learningSituationCrudRepository.save(new LearningSituation(null, user.getId(), trainingPlanId, documentVideoId, percentage));
        return Result.success(save);
    }

    @PostMapping("/getLearningSituationOfOnePlan")
    public Result getLearningSituationOfOnePlan(@RequestBody LearningSituationDTO learningSituationDTO,
                                                @RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {

        //log.info("learningSituationDTO,{}",learningSituationDTO);
        List<LearningSituationVo> situations = new ArrayList<>();
        Integer trainingPlanId = learningSituationDTO.getTrainingPlanId();
        if(trainingPlanId==null){
            return Result.error("请先选择培训计划");
        }
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "id");
        //查询满足条件的所有学员
        List<User> allStudent=new ArrayList<>();
        Page<User> userPage = userCrudRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (learningSituationDTO != null) {
                    // 根据 user 的属性添加查询条件
                    if (learningSituationDTO.getName() != null) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + learningSituationDTO.getName() + "%"));
                    }
                    predicate.getExpressions().add(cb.equal(root.get("role"), "学员"));
                    if (learningSituationDTO.getAgencyId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("agencyId"), learningSituationDTO.getAgencyId()));
                    }
                }
                return predicate;
            }
        }, pageable);
        allStudent = userPage.getContent();

        //查询参数计划下的所有学员对应的学习记录
        for (User user : allStudent) {
            List<LearningSituation> userLearningSituation = learningSituationCrudRepository.getLearningSituationByTrainingPlanIdAndUserId(trainingPlanId, user.getId());
            LearningSituationVo vo = new LearningSituationVo(user.getId(), trainingPlanId, user.getName(), user.getAgencyId(), userLearningSituation);
            situations.add(vo);
           // log.info("userLearningSituation,{}",userLearningSituation);
        }


        long elements = userPage.getTotalElements();
        return Result.success(new PageBean(elements, situations));
    }


    @PostMapping("/getLearningSituationList")
    public Result getLearningSituationList(@RequestBody LearningSituationDTO learningSituationDTO) {

        //log.info("learningSituationDTO,{}",learningSituationDTO);
        List<LearningSituationVo> situations = new ArrayList<>();
        Integer trainingPlanId = learningSituationDTO.getTrainingPlanId();
        if(trainingPlanId==null){
            return Result.error("请先选择培训计划");
        }
        //查询满足条件的所有学员
        List<User> allStudent = userCrudRepository.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (learningSituationDTO != null) {
                    // 根据 user 的属性添加查询条件
                    if (learningSituationDTO.getName() != null) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + learningSituationDTO.getName() + "%"));
                    }
                    predicate.getExpressions().add(cb.equal(root.get("role"), "学员"));
                    if (learningSituationDTO.getAgencyId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("agencyId"), learningSituationDTO.getAgencyId()));
                    }
                }
                return predicate;
            }
        });

        //查询参数计划下的所有学员对应的学习记录
        for (User user : allStudent) {
            List<LearningSituation> userLearningSituation = learningSituationCrudRepository.getLearningSituationByTrainingPlanIdAndUserId(trainingPlanId, user.getId());
            LearningSituationVo vo = new LearningSituationVo(user.getId(), trainingPlanId, user.getName(), user.getAgencyId(), userLearningSituation);
            situations.add(vo);
            // log.info("userLearningSituation,{}",userLearningSituation);
        }

        return Result.success(situations);
    }

}
